package cn.yeyiu.util;

import androidx.lifecycle.MutableLiveData;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadUtil {
    public static ExecutorService pool = Executors.newFixedThreadPool(10);

    public static void run(Runnable runnable) {
        pool.submit(runnable);
    }

    public static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (Exception ignore) {
        }
    }

    public static <T> T waitLdValue(MutableLiveData<T> data) {
        while (data.getValue() == null) {
            sleep(500);
        }
        return data.getValue();
    }

    /**
     * 线程的方式 等待并获取 data value 然后 传递给 poster
     * @param poster 目标
     * @param data 数据源
     * @param <T> 数据类型
     */
    public static <T> void waitPostLdValue(MutableLiveData<T> poster, MutableLiveData<T> data) {
        run(()->{
            poster.postValue(waitLdValue(data));
        });
    }
}
